define(['app/mdSheQuHospitalBase/sale/sale.model', 'app/mdSheQuHospitalBase/sale/sale.service', 'hr.common'
, 'app/mdSheQuHospitalBase/bill/bill.service'
, 'app/mdSheQuHospitalBase/stock/stock.service'
], function (Model, service, common
, billService
, stockService
) {
    function initialize(){
        var model = new Model();
        var initParam = model.tableModel.param();

        billService.findAll().success(function(data){
            model.dialogModel.billArray(data.data || []);
        });

        stockService.findAll().success(function(data){
            model.dialogModel.stockArray(data.data || []);
        });

        /**
         * 查询
         */
        model.searchData = function(data, event){
            var param = '';
            param += 'search_EQ_buyQuantity=' + ko.unwrap(data.searchModel.buyQuantity()) + '&';
            param += 'search_EQ_unitPrice=' + ko.unwrap(data.searchModel.unitPrice()) + '&';
            param += 'search_EQ_receiveTotal=' + ko.unwrap(data.searchModel.receiveTotal()) + '&';
            param += 'search_EQ_bill.id=' + ko.unwrap(data.searchModel.bill().id) + '&';
            param += 'search_EQ_stock.id=' + ko.unwrap(data.searchModel.stock().id);
            if(initParam) {
                param = initParam + '&' + param;
            }
            data.tableModel.param(param);
            model.tableModel.loadData(true);
        };

        /**
         * 详细
         */
        model.showData = function (data, event) {
            var viewModel = new Model().dialogModel;
            viewModel.saleModel = common.mappingData(common.extend(viewModel.saleModel, data));
            var dia = dialog({
                id: "showSaleDialog",
                url: '/mdSheQuHospitalBase/sale/sale.show.tmpl.html',
                width: '600px',
                title: '销售表基本信息',
                okValue: '关闭',
                ok: function () {
 
                },
                onshow: function () {
 
                }
            });
            common.showModalDialog(dia, viewModel);
        };

        /**
         * 增加
         */
        model.addData = function(){
            var viewModel = new Model().dialogModel;
            modifyData(viewModel, '新建');
            model.tableModel.loadData(true);
        };

        /**
         * 修改
         */
        model.updateData = function (data, event) {
            var viewModel = new Model().dialogModel;
            viewModel.saleModel = common.mappingData(common.extend(viewModel.saleModel, data), ['bill', 'stock']);
            modifyData(viewModel, '编辑');
        };

        /**
         * 增加或修改
         */
        function modifyData(viewModel, titlePrefix){
            viewModel.billArray(model.dialogModel.billArray());
            viewModel.stockArray(model.dialogModel.stockArray());
            var dia = dialog({
                id: 'saveSaleDialog',
                url: '/mdSheQuHospitalBase/sale/sale.save.tmpl.html',
                width: '600px',
                title: titlePrefix + '销售表',
                okValue: '确定',
                cancelValue: '取消',
                ok: function () {
                    if(common.isErrors(viewModel.saleModel)){
                        var param = ko.mapping.toJSON(viewModel.saleModel);
                        service.saveData(param).success(function(data){
                            common.showAlertDialog('数据保存成功!', 'success');
                            model.tableModel.loadData(true);
                        });
                    }else{
                        return false;
                    }
                },
                cancel: function () {},
                onshow: function(){

                }
            });
            common.showModalDialog(dia, viewModel);
        }

        /**
         * 删除
         */
        model.deleteData = function (data, event) {
            common.showConfirmDialog('确定要删除吗?', function () {
                service.deleteDataById(data.id).success(function(data){
                    common.showAlertDialog('数据删除成功!', 'success');
                    model.tableModel.loadData(true);
                });
            });
        };

        return model;
    }
    return {
        initialize: initialize
    };
});